/**
 *====================================================
 * 文件名称: FinanceCreditServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年11月19日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.member.entity.lexicon.AuditStateEnum;
import com.yuexunit.finance.project.entity.FinanceCreditEntity;
import com.yuexunit.finance.project.entity.lexicon.AdjustmentEnum;
import com.yuexunit.finance.project.entity.lexicon.ReplenishStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: FinanceCreditServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年11月19日 17:29:55
 */
@Implements(FinanceCreditService.class)
public class FinanceCreditServiceImpl implements FinanceCreditService {

	@Override
	public Long createFinanceCredit(FinanceCreditEntity entity) {
		entity.setCreateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getCreditId();
	}

	@Override
	@Transactional
	public void updateFinanceCredit(FinanceCreditEntity entity) {
		FinanceCreditEntity persist = inquireFinanceCredit(entity.getCreditId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(entity);
	}

	@Override
	public void renovateFinanceCredit(FinanceCreditEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public FinanceCreditEntity inquireFinanceCredit(Long creditId) {
		return PersistUtil.get(FinanceCreditEntity.class, creditId);
	}

	@Override
	public PageList<FinanceCreditEntity> inquireFinanceCreditPageList(AdjustmentEnum adjustment, Long financeId, AuditStateEnum auditState, Long creditId, Long enterpriseId, Long loanNoticeId, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceCreditEntity.class);
		if (financeId != null && financeId != 0) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}

		if (creditId != null && creditId != 0) {
			criteria.add(Restrictions.eq("creditId", creditId));
		}
		
		if (loanNoticeId != null && loanNoticeId != 0) {
			criteria.add(Restrictions.eq("loanNoticeId", loanNoticeId));
		}
		
		if (enterpriseId != null && enterpriseId != 0) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}

		if (auditState != null && auditState.ordinal() != 0) {
			criteria.add(Restrictions.eq("auditState", auditState));
		}
		
		criteria.add(Restrictions.eq("adjustment", adjustment));
		criteria.addOrder(Order.desc("createDateTime"));
		return PersistUtil.get(criteria, page);
	}
	
	@Override
	public PageList<FinanceCreditEntity> inquireFinanceCreditPageLists(AdjustmentEnum adjustment, Long financeId, AuditStateEnum auditState, Long creditId, Long enterpriseId, Long loanNoticeId, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceCreditEntity.class);
		if (financeId != null && financeId != 0) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}

		if (creditId != null && creditId != 0) {
			criteria.add(Restrictions.eq("creditId", creditId));
		}
		
		if (loanNoticeId != null && loanNoticeId != 0) {
			criteria.add(Restrictions.eq("loanNoticeId", loanNoticeId));
		}
		
		if (enterpriseId != null && enterpriseId != 0) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}

		if (auditState != null && auditState.ordinal() != 0) {
			criteria.add(Restrictions.eq("auditState", auditState));
		}
		
		criteria.add(Restrictions.eq("adjustment", adjustment));
		criteria.addOrder(Order.desc("createDateTime"));
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<Long> countReplenishAudit() {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceCreditEntity.class);
		criteria.add(Restrictions.eqOrIsNull("adjustment", AdjustmentEnum.REPLENISH));
		criteria.add(Restrictions.eqOrIsNull("auditState", AuditStateEnum.UNAUDIT));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("creditId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countReplenishConfirm() {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceCreditEntity.class);
		criteria.add(Restrictions.eqOrIsNull("adjustment", AdjustmentEnum.REPLENISH));
		criteria.add(Restrictions.eqOrIsNull("replenishState", ReplenishStateEnum.WAIT));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("creditId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countBailAdditionAudits() {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceCreditEntity.class);
		criteria.add(Restrictions.eqOrIsNull("adjustment", AdjustmentEnum.DESPOIST));
		criteria.add(Restrictions.eqOrIsNull("auditState", AuditStateEnum.UNAUDIT));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("creditId")));
		return PersistUtil.get(criteria);
	}
}
